package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.TaskReceiver;
import java.util.List;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author dimitri
 */
public class TaskReceiverEditPanel extends javax.swing.JPanel {

    /** Creates new form ReceiverPanel */
    public TaskReceiverEditPanel() {
        initComponents();
        list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                enableButtons();
            }
        });
        enableButtons();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAddUser = new javax.swing.JButton();
        btnAddGroup = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnUp = new javax.swing.JButton();
        btnDown = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        list = new ge.telasi.tasks.ui.task.TaskReceiverList();

        btnAddUser.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ge/telasi/resorces/icons16x16/user_add.png"))); // NOI18N
        btnAddUser.setText("თანამშრომელი");
        btnAddUser.setToolTipText("თანამშრომლის დამატება");
        btnAddUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddUserActionPerformed(evt);
            }
        });

        btnAddGroup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ge/telasi/resorces/icons16x16/group_add.png"))); // NOI18N
        btnAddGroup.setText("ჯგუფი");
        btnAddGroup.setToolTipText("თანამშრომელთა ჯგუფის დამატება");
        btnAddGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddGroupActionPerformed(evt);
            }
        });

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ge/telasi/resorces/icons16x16/delete.png"))); // NOI18N
        btnDelete.setToolTipText("სიიდან წაშლა");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnUp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ge/telasi/resorces/icons16x16/arrow_up.png"))); // NOI18N
        btnUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpActionPerformed(evt);
            }
        });

        btnDown.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ge/telasi/resorces/icons16x16/arrow_down.png"))); // NOI18N
        btnDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDownActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(list);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(btnAddUser)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAddGroup)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
                .addComponent(btnUp)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDown)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDelete))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnAddUser)
                        .addComponent(btnAddGroup)
                        .addComponent(btnDelete))
                    .addComponent(btnDown)
                    .addComponent(btnUp)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddUserActionPerformed
        list.addUser();
    }//GEN-LAST:event_btnAddUserActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        list.removeReceivers();
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnAddGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddGroupActionPerformed
        list.addGroup();
    }//GEN-LAST:event_btnAddGroupActionPerformed

    private void btnUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpActionPerformed
        list.moveUp();
    }//GEN-LAST:event_btnUpActionPerformed

    private void btnDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDownActionPerformed
        list.moveDown();
    }//GEN-LAST:event_btnDownActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddGroup;
    private javax.swing.JButton btnAddUser;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnDown;
    private javax.swing.JButton btnUp;
    private javax.swing.JScrollPane jScrollPane1;
    private ge.telasi.tasks.ui.task.TaskReceiverList list;
    // End of variables declaration//GEN-END:variables

    private void enableButtons() {
        int index = list.getSelectedIndex();
        int size = list.getModel().getSize();
        boolean selected = index != -1;
        btnDelete.setEnabled(selected);
        btnUp.setEnabled(selected && index > 0);
        btnDown.setEnabled(selected && index < size - 1);
    }

    public List<TaskReceiver> getReceivers() {
        return list.getReceivers();
    }

    public void setReceivers(List<TaskReceiver> receivers) {
        list.setReceivers(receivers);
    }

}
